package com.atguigu.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.atguigu.commonutils.ResultVo;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.TeacherQueryVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-12-09
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin

@Api(tags="课程管理")
public class EduCourseController {




    @Autowired
    private EduCourseService courseService;
    //课程列表  基本实现
    //自己写完善条件查询带分页
    @GetMapping("getCourseList")
    public ResultVo getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return ResultVo.ok().data("list",list);
    }



    //添加课程基本信息
    @ApiOperation(value = "新增课程")
    @PostMapping("/addCourseInfo")
    public ResultVo addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后的课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return ResultVo.ok().data("courseId",id);
    }
    //根据课程id查询课程基本信息
    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("getCourseInfo/{courseId}")
    public ResultVo getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return ResultVo.ok().data("courseInfoVo",courseInfoVo);
    }
    @ApiOperation(value = "更新课程")
    @PostMapping("updateCourseInfo")
    public ResultVo updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return ResultVo.ok();
    }
    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public ResultVo getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo= courseService.getPublishCourseInfo(id);
        return ResultVo.ok().data("publishCourse",coursePublishVo);
    }
    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public ResultVo publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        courseService.updateById(eduCourse);
        return ResultVo.ok();
    }
    //课程删除
    @DeleteMapping("{courseId}")
    public ResultVo deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return ResultVo.ok();
    }

//    //4.带条件查询的分页方法
//    // 带条件分页查询讲师
//    @ApiOperation(value = "带条件分页查询讲师")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name="current",value = "当前页"),
//            @ApiImplicitParam(name="limit",value = "每页记录")
//    })
//    @PostMapping("pageTeacherCondition/{current}/{limit}")
//    public ResultVo pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) TeacherQueryVo teacherQueryVo){
//        //创建page对象
//        Page<EduTeacher> teacherPage =new Page<>(current,limit);
//        //构建条件
//        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
//        //多条件组合查询
//        String name = teacherQueryVo.getName();
//        Integer level = teacherQueryVo.getLevel();
//        String begin = teacherQueryVo.getBegin();
//        String end = teacherQueryVo.getEnd();
//        //判断条件值是否为空，如果不为空拼接条件
//        if(!org.apache.commons.lang3.StringUtils.isEmpty(name)){
//            wrapper.like("name",name);
//        }
//        if(level!=null){
//            wrapper.eq("level",level);
//        }
//        if(!org.apache.commons.lang3.StringUtils.isEmpty(begin)){
//            wrapper.ge("gmt_create", begin);
//        }
//        if(!org.apache.commons.lang3.StringUtils.isEmpty(end)){
//            wrapper.le("gmt_create", end);
//        }
//        //排序
//        wrapper.orderByDesc("gmt_create");
//        //调用方法实现分页
//        //调用方法时，底层封装，把分页所有数据封装到teacherPage对象里面
//        teacherService.page(teacherPage,wrapper);
//        //总记录数
//        long total = teacherPage.getTotal();
//        //数据list集合
//        List<EduTeacher> records = teacherPage.getRecords();
//        Map map=new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return ResultVo.ok().data(map);
//    }
    @ApiOperation(value = "条件查询分页讲师列表")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public ResultVo pageTeacherCondition(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)//参数值可以为空
            @RequestBody CourseQuery courseQuery
    ){
        //创建page对象
        Page<EduCourse> pageTeacher = new Page<>(current,limit);
        //构造条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件分页查询
        //调用方法时，底层封装，把分页所有数据封装到teacherPage对象里面
        courseService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<EduCourse> records = pageTeacher.getRecords();//数据list集合
        Map map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);

        return ResultVo.ok().data(map);
    }
}

